package com.fulihui.usercore.service;

import com.fulihui.usercore.api.ThirdMerchantService;
import com.fulihui.usercore.common.util.Errors;
import com.fulihui.usercore.condition.ThirdMerchantCondition;
import com.fulihui.usercore.core.repository.ThirdMerchantRepository;
import com.fulihui.usercore.dto.ThirdMerchantDTO;
import com.fulihui.usercore.request.merchant.ThirdMerchantSaveRequest;
import org.apache.dubbo.config.annotation.Service;
import org.near.servicesupport.error.CommonServiceErrorEnum;
import org.near.servicesupport.error.InvokeServiceException;
import org.near.servicesupport.request.TRequest;
import org.near.servicesupport.result.BaseResult;
import org.near.servicesupport.result.ResultBuilder;
import org.near.servicesupport.result.TMultiResult;
import org.near.servicesupport.result.TSingleResult;
import org.near.servicesupport.util.ServiceAssert;
import org.near.toolkit.common.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author Willard.Hu on 2016/9/22 0022.
 */
@Service(version = "1.0.0")
public class ThirdMerchantServiceImpl implements ThirdMerchantService {

    @Autowired
    private ThirdMerchantRepository thirdMerchantRepository;

    /**
     * @see ThirdMerchantService#save(ThirdMerchantSaveRequest)
     */
    @Override
    public TSingleResult<Integer> save(ThirdMerchantSaveRequest request) {
        int id = request.getId();
        if (request.getId() == -1) {
            ServiceAssert.notBlank(request.getName(),
                CommonServiceErrorEnum.REQUEST_PARAMETER_ERROR);
            ServiceAssert.notBlank(request.getBizType(),
                CommonServiceErrorEnum.REQUEST_PARAMETER_ERROR);
            checkName(request.getName(), request.getBizType(), -1);
            id = thirdMerchantRepository.insert(request);
        } else {
            if (StringUtil.isNotBlank(request.getName())) {
                checkName(request.getName(), request.getBizType(), request.getId());
            }
            boolean b = thirdMerchantRepository.update(request);
            if (!b) {
                ResultBuilder.fail(BaseResult.class, Errors.Merchant.THIRD_MERCHANT_OPERATE_ERROR);
            }
        }
        return ResultBuilder.succTSingle(id);
    }

    /**
     * 验证商户名字不能重复
     * @param name 商户名字
     * @param bizType 业务类型
     * @param id 主键
     */
    private void checkName(String name, String bizType, int id) {
        ThirdMerchantCondition condition = new ThirdMerchantCondition();
        condition.setName(name);
        condition.setBizType(bizType);
        List<ThirdMerchantDTO> list = thirdMerchantRepository.query(condition);
        ThirdMerchantDTO dto = CollectionUtils.isEmpty(list) ? null : list.get(0);
        if (dto != null && dto.getId() != id) {
            throw new InvokeServiceException(Errors.Merchant.MERCHANT_NAME_HAS_EXISTS);
        }
    }

    /**
     * @see ThirdMerchantService#delete(TRequest)
     */
    @Override
    public BaseResult delete(TRequest<Integer> request) {
        ServiceAssert.notNull(request.getCondition(),
            CommonServiceErrorEnum.REQUEST_PARAMETER_ERROR);
        boolean b = thirdMerchantRepository.delete(request.getCondition());
        return b ? ResultBuilder.succ(BaseResult.class) : ResultBuilder.fail(BaseResult.class,
            Errors.Merchant.THIRD_MERCHANT_OPERATE_ERROR);
    }

    /**
     * @see ThirdMerchantService#queryByPK(TRequest)
     */
    @Override
    public TSingleResult<ThirdMerchantDTO> queryByPK(TRequest<Integer> request) {
        ServiceAssert.notNull(request.getCondition(),
            CommonServiceErrorEnum.REQUEST_PARAMETER_ERROR);
        ThirdMerchantDTO dto = thirdMerchantRepository.queryByPK(request.getCondition());
        return ResultBuilder.succTSingle(dto);
    }

    /**
     * @see ThirdMerchantService#query(TRequest)
     */
    @Override
    public TMultiResult<ThirdMerchantDTO> query(TRequest<ThirdMerchantCondition> request) {
        List<ThirdMerchantDTO> dtoList = thirdMerchantRepository.query(request.getCondition());
        return ResultBuilder.succTMulti(dtoList);
    }
}
